import { IDataServiceRegister, IParam } from '@core';

/**
 * 数据服务注册中心
 *
 * @export
 * @class DataServiceRegister
 */
export class DataServiceRegister implements IDataServiceRegister {

    /**
     * DataServiceRegister 单例对象
     *
     * @private
     * @static
     * @memberof DataServiceRegister
     */
    private static DataServiceRegister: DataServiceRegister;

    /**
     * 所有数据服务 Map对象
     *
     * @private
     * @static
     * @memberof DataServiceRegister
     */
    private static allDataServiceMap: Map<string, any> = new Map();

    /**
     * Creates an instance of DataServiceRegister.
     * @memberof DataServiceRegister
     */
    constructor() {
        this.init();
    }

    /**
     * 获取DataServiceRegister 单例对象
     *
     * @public
     * @static
     * @memberof DataServiceRegister
     */
    public static getInstance() {
        if (!this.DataServiceRegister) {
            this.DataServiceRegister = new DataServiceRegister();
        }
        return this.DataServiceRegister;
    }

    /**
     * 初始化
     *
     * @protected
     * @memberof DataServiceRegister
     */
    protected init(): void {
        DataServiceRegister.allDataServiceMap.set('complianceguide', () => import('@api/compliance-guide/compliance-guide-service'));
        DataServiceRegister.allDataServiceMap.set('lawsupport', () => import('@api/law-support/law-support-service'));
        DataServiceRegister.allDataServiceMap.set('lawpropagandize', () => import('@api/law-propagandize/law-propagandize-service'));
        DataServiceRegister.allDataServiceMap.set('privatelegaldatashare', () => import('@api/private-legal-data-share/private-legal-data-share-service'));
        DataServiceRegister.allDataServiceMap.set('sysemployee', () => import('@api/sys-employee/sys-employee-service'));
        DataServiceRegister.allDataServiceMap.set('hotlawcase', () => import('@api/hot-law-case/hot-law-case-service'));
        DataServiceRegister.allDataServiceMap.set('deptinterface', () => import('@api/dept-interface/dept-interface-service'));
        DataServiceRegister.allDataServiceMap.set('sysrole', () => import('@api/sys-role/sys-role-service'));
        DataServiceRegister.allDataServiceMap.set('sysuserrole', () => import('@api/sys-user-role/sys-user-role-service'));
        DataServiceRegister.allDataServiceMap.set('systodo', () => import('@api/sys-todo/sys-todo-service'));
        DataServiceRegister.allDataServiceMap.set('lawexpress', () => import('@api/law-express/law-express-service'));
        DataServiceRegister.allDataServiceMap.set('complianceissue', () => import('@api/compliance-issue/compliance-issue-service'));
    }

    /**
     * 获取指定数据服务
     *
     * @param {string} entityKey 应用实体名小写
     * @param {*} context 应用上下文
     * @return {*} 
     * @memberof DataServiceRegister
     */
    public async getService(entityKey: string, context: IParam = {}) {
        const importService = DataServiceRegister.allDataServiceMap.get(entityKey);
        if (importService) {
            const importModule = await importService();
            return importModule.default.getInstance(context);
        }
    }

}